<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class Stock extends Model
{
    protected $table = 'stock';

    /** 入库出库归还更新库存表
     * @param $goods_id
     * @param $material_id
     * @param $checkcategory_id
     * @param $company_id
     * @param $number
     * @param $type:1：入库；2：出库
     * @param $classify:入库类型1(1:新增入库；2：盘点入库；3：归还入库;);2:出库类型（1：领用出库；2：盘点出库：3：报废出库）
     * @return mixed
     */
    public static function operate($goods_id=0,$material_id=0,$checkcategory_id=0,$company_id=0,$type=1,$classify=1,$other=[]){
        $stock_data = self::where('goods_id',$goods_id)->where('material_id',$material_id)->where('checkcategory_id',$checkcategory_id)->where('company_id',$company_id);
        $query = $stock_data->first();
        $update_order = self::warehouse($goods_id,$material_id,$checkcategory_id,$company_id,$type,$classify);

        if($update_order['warehouse']>=0){
            if($query){
                $res = $stock_data->update($update_order);
            }else{
                $update_order['goods_id'] = $goods_id;
                $update_order['material_id'] = $material_id;
                $update_order['checkcategory_id'] = $checkcategory_id;
                $update_order['company_id'] = $company_id;
                if($other){
                    foreach ($other as $k=>$v){
                        $update_order[$k] = $v;
                    }
                }
                $update_order['created_at'] = date('Y-m-d H:i:s');
                $res = self::insertGetId($update_order);
            }
            MaterialList::operate($material_id);
            Goods::operate($goods_id);
            return $res;
        }else{
            return 0;
        }


    }

    /**
     * @param $goods_id
     * @param $material_id
     * @param $checkcategory_id
     * @param $company_id
     * @param $type:1：入库；2：出库
     * @param $classify:入库类型1(1:新增入库；2：盘点入库；3：归还入库;);2:出库类型（1：领用出库；2：盘点出库：3：报废出库）
     * @return array
     */
    public static function warehouse($goods_id=0,$material_id=0,$checkcategory_id=0,$company_id=0,$type=1,$classify=1){
        $update_order = [];
        $warehouseMaterials = WarehouseMaterials::getMaterialsNum($goods_id,$material_id,$checkcategory_id,$company_id,$classify);
        $ins_all = $warehouseMaterials['ins_all'];
        $ins = $warehouseMaterials['ins'];

        $getOutBoundNum = WarehouseOutboundDetail::getOutBoundNum($goods_id,$material_id,$checkcategory_id,$company_id,$classify);
        $outboundNum_all = $getOutBoundNum['outboundNum_all'];
        $outboundNum = $getOutBoundNum['outboundNum'];
        if($type==1){ //入库
            if($classify==1 || $classify==2){  //1:新增入库；2：盘点入库；
                $update_order['income_warehouse']=$ins;
                $update_order['warehouse']=$ins_all-$outboundNum_all;
            }
            /*if($type==2){
                $update_order['loss_warehouse']=$ins;
                $update_order['warehouse']=$ins_all-$outboundNum_all;
            }*/
            if($classify==3){  //3：归还入库;
                $update_order['leave_warehouse']=$ins;
                $update_order['warehouse']=$ins_all-$outboundNum_all;
            }

        }else{  //出库
            if($classify==1 || $classify==2){ //1：领用出库；2：盘点出库：
                $update_order['leave_warehouse']=$outboundNum;
                $update_order['warehouse']=$ins_all-$outboundNum_all;
            }
            if($classify==3){  //报废出库
                $update_order['loss_warehouse']=$outboundNum;
                $update_order['warehouse']=$ins_all-$outboundNum_all;
            }
        }
        return $update_order;
    }

    /**
     * @param $id:($type=1为入库单id，$type=2为出库单id)
     * @param $type:1：入库；2：出库
     * @param $classify入库类型1(1:新增入库；2：盘点入库；3：归还入库;);2:出库类型（1：领用出库；2：盘点出库：3：报废出库）
     * @return mixed
     */
    public static function operate_del($id=0,$type=1){
        $goods_id=$material_id=0;
        if($type==1){
            $data = WarehouseMaterials::where('order_id',$id)->get()->toarray();
            foreach ($data as $key=>$val){
                $goods_id=$val['goods_id'];
                $material_id=$val['material_id'];
                $stock_data = self::where('goods_id',$val['goods_id'])->where('material_id',$val['material_id'])->where('checkcategory_id',$val['checkcategory_id'])->where('company_id',$val['company_id']);
                $query = $stock_data->first();
                if($query){
                    $update_order = self::warehouse_del($val['goods_id'],$val['material_id'],$val['checkcategory_id'],$val['company_id'],$type,$val['warehouse_type'],$val['number']);
                    $res = $stock_data->update($update_order);
                    MaterialList::operate($material_id);
                    Goods::operate($goods_id);
                }
                WarehouseMaterials::where('id', $val['id'])->delete();
            }
        }else{
            $data = WarehouseOutboundDetail::where('outbound_id',$id)->get()->toarray();
            foreach ($data as $key=>$val){

                $stock_data = self::where('id',$val['stock_id']);
                $query = $stock_data->first();
                if($query){
                    $update_order = self::warehouse_del($query['goods_id'],$query['material_id'],$query['checkcategory_id'],$query['company_id'],$type,$val['outbound_type'],$val['number']);
                    $res = $stock_data->update($update_order);
                    $goods_id=$query['goods_id'];
                    $material_id=$query['material_id'];
                    MaterialList::operate($material_id);
                    Goods::operate($goods_id);
                }
                WarehouseOutboundDetail::where('id', $val['id'])->delete();
            }
        }

        return 1;
    }

    /**
     * @param $goods_id
     * @param $material_id
     * @param $checkcategory_id
     * @param $company_id
     * @param $type:1：入库；2：出库
     * @param $classify:入库类型1(1:新增入库；2：盘点入库；3：归还入库;);2:出库类型（1：领用出库；2：盘点出库：3：报废出库）
     * @return array
     */
    public static function warehouse_del($goods_id=0,$material_id=0,$checkcategory_id=0,$company_id=0,$type=1,$classify=1,$number=0){
        $update_order = [];
        $warehouseMaterials = WarehouseMaterials::getMaterialsNum($goods_id,$material_id,$checkcategory_id,$company_id,$classify);
        $ins_all = $warehouseMaterials['ins_all'];
        $ins = $warehouseMaterials['ins'];//var_dump($warehouseMaterials);

        $getOutBoundNum = WarehouseOutboundDetail::getOutBoundNum($goods_id,$material_id,$checkcategory_id,$company_id,$classify);
        $outboundNum_all = $getOutBoundNum['outboundNum_all']; //var_dump($getOutBoundNum);
        $outboundNum = $getOutBoundNum['outboundNum'];
        if($type==1){ //入库
            if($classify==1 || $classify==2){
                $update_order['income_warehouse']=$ins-$number;
                $update_order['warehouse']=$ins_all-$number-$outboundNum_all;
            }
            /*if($type==2){
                $update_order['loss_warehouse']=$ins+$number;
                $update_order['warehouse']=$ins-$number;
            }*/
            if($type==3){
                $update_order['leave_warehouse']=$ins-$number;
                $update_order['warehouse']=$ins_all-$number-$outboundNum_all;
            }

        }else{  //出库
            if($classify==1 || $classify==2){ //1：领用出库；2：盘点出库：
                $update_order['leave_warehouse']=$outboundNum-$number;
                $update_order['warehouse']=$ins_all+$number-$outboundNum_all;
            }
            if($classify==3){  //报废出库
                $update_order['loss_warehouse']=$outboundNum-$number;
                $update_order['warehouse']=$ins_all+$number-$outboundNum_all;
            }
        }
        //var_dump($update_order);
        return $update_order;
    }
    //所属单位分类
    public function checkcategory()
    {
        return $this->belongsTo('App\Model\Checkcategory','checkcategory_id');
    }
    //物资分类
    public function goods()
    {
        return $this->belongsTo('App\Model\Goods','goods_id');
    }
    //物资名称
    public function materialList()
    {
        return $this->belongsTo('App\Model\MaterialList','material_id');
    }
    //单位名称
    public function company()
    {
        return $this->belongsTo('App\Model\Company','company_id');
    }
    //入库明细
    public function warehouseMaterials()
    {
        return $this->hasMany('App\Model\WarehouseMaterials','material_id');
    }
    //库存状态
    public static function get_status($type=0){
        $data = ['1'=>'在库','2'=>'出库','3'=>'损耗'];
        return $data[$type]??$data;
    }
}
